package maincode;
import java.util.*;
import java.io.*;
public class iniReader {
	public static boolean checkFileInt() throws IOException
	{	
		File infile=new File("D:\\latest.txt");
		if(!infile.isFile())
			return false;
		for(int i=0;i<=5;i++)
		{
			File temInfile=new File("D:\\data"+i+".txt");
			if(!temInfile.isFile())
				return false;
		}
		return true;
	}
	
	public static void checkFile() throws IOException
	{
		Long d=new Date().getTime()/(24*1000*3600);
//		System.out.println(d);
		File infile=new File("D:\\latest.txt");
		Scanner in=new Scanner(infile);
		Long rec=in.nextLong();
		in.close();
		if(rec!=d)
		{
			System.out.println("Getting data...");
			for(int i=0;i<=5;i++)
			{
				FileWriter outfile=new FileWriter("D:\\tem.txt");
				
				PrintWriter writeHelper=new PrintWriter(outfile);
				File temInfile=new File("D:\\data"+i+".txt");
				Scanner temIn=new Scanner(temInfile);
				//todo-get data from website
				for(Long t=d;t>rec;t--)
				{
					Long ind=t;
					double res=dataOfDate.getDataAtDate(ind, i);
					while(res==-1)
					{
						res=dataOfDate.getDataAtDate(ind, i);
						ind++;
//						System.out.println("getData! "+i);
					}
					writeHelper.println(res);
//					System.out.println(res);
//					System.out.println(""+i+" "+t);
				}
				for(Long t=d-rec+1;t<=100;t++)
				{
					writeHelper.println(temIn.nextDouble());
				}
				
				temIn.close();
				writeHelper.close();
				temInfile.delete();
				File newfile=new File("D:\\tem.txt");
				newfile.renameTo(new File("D:\\data"+i+".txt"));
			}
			FileWriter latestfile=new FileWriter(infile);
			PrintWriter writeHelper=new PrintWriter(latestfile);
			writeHelper.println(d);	
			writeHelper.close();
			System.out.println("Finished!");
		}
		//to-do change data record accordingl
	}
	
	public static boolean readFiles() throws IOException
	{
		if(!checkFileInt())
		{
			System.out.println("file is lacking");
			return false;
		}
		checkFile();
		String[] names={"usd","cny","jpy","eur","aud","gbp"};
		double lists [][] = new double[6][];
		double rateLists[][] =new double[6][];
		for(int i=0;i<=5;i++)
		{	
			lists[i]=new double[99];//to change to CS version
			rateLists[i]=new double[100];//keep the valus of rate
		}
		Scanner in;
		for(int i=0;i<=5;i++)
		{
			File infile=new File("D:\\data"+i+".txt");
			in=new Scanner(infile);		
			double pre=in.nextDouble();
			rateLists[i][0]=pre;
			for(int j=0;j<=98;j++)
			{
				double cur=in.nextDouble();
				rateLists[i][j+1]=cur;
				lists[i][j]=(cur-pre)/cur;
				pre=cur;
			}
			currencyStream temCS=new currencyStream(i,names[i],lists[i],rateLists[i]);
			currencyStream.setCurSet(temCS, i);
			in.close();
		}
		return true;
	}
}
